import router from "@/router";
export default {
    state: {
        listTag: [{ //当前显示标签
            path: "/index/home",
            name: "home",
            meta: {
                title: "home"
            },
            query: {}
        }]
    },
    getters: {},
    mutations: {
        // 新增标签列表
        addList(state, to) {
            let tagAdd = {
                path: to.path,
                name: to.name,
                meta: to.meta,
                query: to.query
            };
            let Tangindex = state.listTag.findIndex((item) => item.path == tagAdd.path) // 查找当前路由是否与传过来的路由相同
            let indexs = state.listTag.findIndex((item) => item.name == "product");  //商品列表所在的位置
            let name = router.currentRoute.name; //拿到name值
            if (-1 == Tangindex && name != "redirect") {
                if (name == 'create' || name == 'edit') {
                    if (indexs != -1) {
                        console.log(state.listTag.splice(indexs + 1, 0, to));
                    } else {
                        router.push({
                            name: "product"
                        })
                    }
                } else {
                    console.log(state.listTag.push(to))
                }
            }
        },
        // 删除标签
        removeList(state, index) {
            let tally = state.listTag;
            let tag = tally[index];
            // 删除当前激活的标签，优先显示后面的标签，如果没有显示前面的标签
            if (router.currentRoute.name == tag.name) {
                //获取要跳转的路由name
                var name = (tally[index + 1] || tally[index - 1]).name
                router.push({ name })
            }
            tally.splice(index, 1);
        },
        //菜单点击事件
        handlerCommand(state, { code, path }) {
            let memu = state.listTag;
            switch (code) {
                case 1:   //刷新
                    const routes = memu.find((item) => item.path === path);
                    router.push({ path: "/index/redirect/" + routes.path });
                    break;
                case 2:  //关闭右侧
                    let indexs = memu.findIndex((item) => item.path == path);
                    let shut = memu.findIndex((item) => item.path === router.currentRoute.path);
                    memu.splice(indexs + 1);
                    if (shut > indexs) {   //当前页面被删除，显示右击菜单页
                        router.push({ path })
                    }
                    break;
                case 3:  //关闭全部
                    state.listTag = memu.filter((item) => item.name == "home"); //关闭全部时，保留首页
                    router.push({ name: "home" });
                    break;
            }
        },

        //单击标签页跳转
        handTag(state, index) {
            let tag = state.listTag[index];
            if (tag.path != router.currentRoute.path) {
                router.push({ path: "/index/redirect/" + tag.path })
            }
        },
        // 点击关闭
        close(state) {
            let indexs = state.listTag.findIndex(
                (item) => item.path == router.currentRoute.path
            ); // 商品管理所在的位置
            state.listTag.splice(indexs, 1); // 如果不等于则删除当前点击的
            router.push({
                name: "product",
            });
        },
    },
    actions: {
        addList(context, route) {
            context.commit("addList", route);
        },
        handlerCommand(context, { code, path }) {
            context.commit("handlerCommand", { code, path });
        },
        removeList(context, index) {
            context.commit("removeList", index);
        },
        handTag(context, index) {
            context.commit("handTag", index);
        },
        close(context) {
            context.commit("handlerClickTag");
        },
    },
    modules: {},

}